import oolongDialog from './confirm';

let oolongDialogInstance;

function getModalInstance() {
  oolongDialogInstance = oolongDialogInstance || oolongDialog.newInstance({
      closable: false,
      maskClosable: false,
      footerHide: true
    });

  return oolongDialogInstance;
}

function confirm(options) {
  let instance = getModalInstance();

  options.onRemove = function () {
    oolongDialogInstance = null;
  };

  instance.show(options);
}

oolongDialog.info = function (props = {}) {
  props.icon = 'info';
  props.showCancel = false;
  return confirm(props);
};

oolongDialog.success = function (props = {}) {
  props.icon = 'success';
  props.showCancel = false;
  return confirm(props);
};

oolongDialog.warning = function (props = {}) {
  props.icon = 'warning';
  props.showCancel = false;
  return confirm(props);
};

oolongDialog.error = function (props = {}) {
  props.icon = 'error';
  props.showCancel = false;
  return confirm(props);
};

oolongDialog.confirm = function (props = {}) {
  props.icon = 'confirm';
  props.showCancel = true;
  return confirm(props);
};

oolongDialog.remove = function () {
  if (!oolongDialogInstance) {   // at loading status, remove after Cancel
    return false;
  }

  const instance = getModalInstance();

  instance.remove();
};

export default oolongDialog;
